package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.NoteTypepxActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.NoteTypeBean;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日程标签对话框
 * Created by liwei on 2017/4/18.
 */

public class NoteTypeDialog extends Dialog {
    private ListView state_lv;

    CallBack callBack;


    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    String userID = "";
    TextView title,fenleiguanli;

    ChooseStateAdapter adapter = null;
    List<NoteTypeBean.ListBean> list = new ArrayList<>();

    App app = App.getDBcApplication();


    public NoteTypeDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager, final NoteTitleDetailBean.TDelListBean bean) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;


        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_flag, null);
        setContentView(view);

        state_lv = (ListView) view.findViewById(R.id.state_lv);
        title = (TextView) view.findViewById(R.id.title);
        fenleiguanli = (TextView) view.findViewById(R.id.fenleiguanli);
        title.setText("修改分类");
        fenleiguanli.setVisibility(View.VISIBLE);
        View footview = LayoutInflater.from(context).inflate(R.layout.emptry_foolterview_white, null);
        state_lv.addFooterView(footview);
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        userID = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.USERID, "0");
        list = App.getDBcApplication().getAllNoteTypeData(userID, false);
        list.remove(0);
//        for (NoteTypeBean.ListBean be: list){
//            if (bean.remark1.equals(be.styleId+"")){
//                list.remove(be);
//                break;
//            }
//        }

        adapter = new ChooseStateAdapter(context, list, bean);
        state_lv.setAdapter(adapter);
        state_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                app.updateNoteChildType(bean.titleId, list.get(position).styleId);
                callBack.updateNoteChildType(list.get(position).styleId);
                dismiss();
            }
        });

        fenleiguanli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NoteTypepxActivity.class);
                intent.putExtra("type",1);
                context.startActivity(intent);
                dismiss();
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);

    }

    public interface CallBack {
        void updateNoteChildType(int styleid);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public class ChooseStateAdapter extends BaseAdapter {
        private Context context;
        private List<NoteTypeBean.ListBean> mList;
        private NoteTitleDetailBean.TDelListBean bean;

        public ChooseStateAdapter(Context context, List<NoteTypeBean.ListBean> mList, NoteTitleDetailBean.TDelListBean bean) {
            this.context = context;
            this.mList = mList;
            this.bean = bean;
        }

        @Override
        public int getCount() {
            return mList == null ? 0 : mList.size();
        }

        @Override
        public Object getItem(int position) {
            return mList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View view = convertView;
            ChooseStateAdapter.ViewWapper viewWapper = null;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(
                        R.layout.adapter_choosestate, null);
                viewWapper = new ChooseStateAdapter.ViewWapper(view);
                view.setTag(viewWapper);
            } else {
                viewWapper = (ChooseStateAdapter.ViewWapper) view.getTag();
            }
            CheckBox state_item_cb = viewWapper.getCheckBox();
            if (bean.remark1.equals(list.get(position).styleId + "")) {
                state_item_cb.setChecked(true);
            }else {
                state_item_cb.setChecked(false);
            }

            TextView state_item_title = viewWapper.getForDayTvTime();
            state_item_title.setText(mList.get(position).content);
            return view;
        }

        class ViewWapper {
            private View view;
            private TextView state_item_title;
            private CheckBox state_item_cb;


            private ViewWapper(View view) {
                this.view = view;
            }

            private TextView getForDayTvTime() {
                if (state_item_title == null) {
                    state_item_title = (TextView) view.findViewById(R.id.state_item_title);
                }

                return state_item_title;
            }

            private CheckBox getCheckBox() {

                if (state_item_cb == null) {
                    state_item_cb = (CheckBox) view.findViewById(R.id.state_item_cb);
                }
                return state_item_cb;
            }
        }
    }
}
